package com.zhiche.lisa.bms.controller.psup;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.psup.PsupOilprice;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.psup.PsupOilPriceVO;
import com.zhiche.lisa.bms.service.psup.PsupOilpriceService;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 人送供方油价维护 前端控制器
 * </p>
 *
 * @author linbao
 * @since 2019-06-18
 */
@RestController
@RequestMapping("/psupOilprice")
@Api(description = "人送市场油价管理", tags = "人送市场油价管理")
@Slf4j
public class PsupOilpriceController extends PsupSdBaseController {

    @Autowired
    private PsupOilpriceService psupOilpriceService;

    /**
     * 分页查询
     *
     * @return
     */
    @PostMapping(value = "/querySdOilPricePage")
    @ApiOperation(value = "人送市场油价分页查询", notes = "人送市场油价分页查询", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PsupOilPriceVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PsupOilPriceVO>> querySdOilPricePage(@RequestBody Page<PsupOilprice> page) {
        log.info("PsupOilpriceController.querySdOilPricePage param: {}", page);
        RestfulResponse<Page<PsupOilPriceVO>> resultResponse = getSuccessResponse();
        try {
            resultResponse.setData(psupOilpriceService.querySdOilPricePage(page));
        } catch (BaseException e) {
            log.error("PsupOilpriceController.querySdOilPricePage error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupOilpriceController.querySdOilPricePage error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 分页查询
     *
     * @return
     */
    @PostMapping(value = "/createSdOilPrice")
    @ApiOperation(value = "人送市场油价分页查询", notes = "人送市场油价分页查询", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PsupOilPriceVO.class),
            @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> createSdOilPrice(@RequestBody PsupOilPriceVO psupOilprice) {
        log.info("PsupOilpriceController.createSdOilPrice param: {}", psupOilprice);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            psupOilpriceService.createSdOilPrice(psupOilprice);
        } catch (BaseException e) {
            log.error("PsupOilpriceController.createSdOilPrice error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupOilpriceController.createSdOilPrice error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @PostMapping(value = "/deleteBatchOilPrice")
    @ApiOperation(value = "人送市场油价批量删除", notes = "人送市场油价批量删除", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> deleteBatchFixedPrice(@RequestBody CommonVO<Long> commonVO) {
        log.info("PsupOilpriceController.deleteBatchOilPrice param: {}", commonVO);
        RestfulResponse<Object> resultResponse = getSuccessResponse();
        try {
            if (Objects.isNull(commonVO) || CollectionUtils.isEmpty(commonVO.getEntityParams())) {
                throw new BaseException("参数为空, 请选择数据!");
            }
            psupOilpriceService.deleteBatchIds(commonVO.getEntityParams());
        } catch (BaseException e) {
            log.error("PsupOilpriceController.deleteBatchOilPrice error:{}", e);
            setBaseExceptionResponse(resultResponse, e.getMessage());
        } catch (Exception e) {
            log.error("PsupOilpriceController.deleteBatchOilPrice error:{} ", e);
            setErrorResponse(resultResponse);
        }
        return resultResponse;
    }
}

